package controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import model.Category;
import model.User;
import org.primefaces.event.FileUploadEvent;

@ManagedBean(name = "fileUploadXML")
@SessionScoped
public class FileUploadXML {

    private String destination = "/home/odiar/Public/file/";//aqui vc seta onde quer gravar o arquivo
    List<String> fileList;
    private static final String OUTPUT_FOLDER = "/home/odiar/Public/file/";

    public void upload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);

        try {
            copyFileXML(event.getFile().getFileName(), event.getFile().getInputstream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    
    public void copyFileXML(String fileName, InputStream in) {
        try {
            FacesContext fc = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
            User u = (User) session.getAttribute("user");
            // write the inputStream to a FileOutputStream
            destination = "/home/odiar/Public/file/use_definition";
            File dir = new File(destination);
            if (!dir.exists()) {
                dir.mkdir();
            }
            fileName = String.valueOf(u.getId()) + ".XML";
            OutputStream out = new FileOutputStream(new File(destination + "/" + fileName));
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            session.setAttribute("localFile", destination+"/"+fileName);

            in.close();
            out.flush();
            out.close();

            System.out.println("New file created!");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
        }
    }

     public void uploadAdmin(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);

        try {
            copyFileXMLAdmin(event.getFile().getFileName(), event.getFile().getInputstream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    
    public void copyFileXMLAdmin(String fileName, InputStream in) {
        try {
            FacesContext fc = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
            User u = (User) session.getAttribute("user");
            // write the inputStream to a FileOutputStream
            destination = "/home/odiar/Public/file/admin_definition";
            File dir = new File(destination);
            if (!dir.exists()) {
                dir.mkdir();
            }
            fileName = String.valueOf(u.getId()) + ".XML";
            OutputStream out = new FileOutputStream(new File(destination + "/" + fileName));
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            session.setAttribute("localFile", destination+"/"+fileName);

            in.close();
            out.flush();
            out.close();

            System.out.println("New file created!");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
        }
    }
}
